import pygame
import sys

# 初始化Pygame
pygame.init()

# 游戏常量配置
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 600
WHITE = (255, 255, 255)
RED = (255, 0, 0)
BLUE = (0, 0, 255)
GRAVITY = 0.8
JUMP_POWER = -15
SPEED = 5

class Stickman:
    def __init__(self, x, y, color, controls):
        # 角色属性
        self.x = x
        self.y = y
        self.color = color
        self.width = 20
        self.height = 40
        self.y_velocity = 0
        self.is_jumping = False
        self.attack_cooldown = 0
        self.score = 0
        
        # 控制键位设置
        self.controls = controls

    def draw(self, screen):
        # 绘制火柴人（基础形状）
        # 头部
        pygame.draw.circle(screen, self.color, (self.x, self.y - 30), 10)
        # 身体
        pygame.draw.line(screen, self.color, (self.x, self.y - 20), (self.x, self.y + 10), 4)
        # 四肢
        pygame.draw.line(screen, self.color, (self.x, self.y), (self.x - 15, self.y + 20), 4)  # 左腿
        pygame.draw.line(screen, self.color, (self.x, self.y), (self.x + 15, self.y + 20), 4)  # 右腿
        pygame.draw.line(screen, self.color, (self.x, self.y - 10), (self.x - 15, self.y - 5), 4)  # 左臂
        pygame.draw.line(screen, self.color, (self.x, self.y - 10), (self.x + 15, self.y - 5), 4)  # 右臂

    def update(self, ground):
        # 应用重力
        self.y_velocity += GRAVITY
        self.y += self.y_velocity
        
        # 地面碰撞检测
        if self.y + self.height/2 >= ground:
            self.y = ground - self.height/2
            self.is_jumping = False
            self.y_velocity = 0
            
        # 攻击冷却
        if self.attack_cooldown > 0:
            self.attack_cooldown -= 1

    def jump(self):
        if not self.is_jumping:
            self.y_velocity = JUMP_POWER
            self.is_jumping = True

    def attack(self, other):
        if self.attack_cooldown <= 0:
            # 简单碰撞检测
            distance = abs(self.x - other.x)
            if distance < 40:
                other.score -= 10
                self.attack_cooldown = 30  # 攻击冷却时间

class Game:
    def __init__(self):
        self.screen = pygame.display.set_mode((SCREEN_WIDTH, SCREEN_HEIGHT))
        pygame.display.set_caption("火柴人大战")
        self.clock = pygame.time.Clock()
        self.ground = SCREEN_HEIGHT - 50
        
        # 创建两个玩家
        self.player1 = Stickman(100, self.ground, RED, {
            'left': pygame.K_a,
            'right': pygame.K_d,
            'jump': pygame.K_w,
            'attack': pygame.K_SPACE
        })
        
        self.player2 = Stickman(700, self.ground, BLUE, {
            'left': pygame.K_LEFT,
            'right': pygame.K_RIGHT,
            'jump': pygame.K_UP,
            'attack': pygame.K_RCTRL
        })

    def handle_events(self):
        keys = pygame.key.get_pressed()
        
        # 玩家1控制
        if keys[self.player1.controls['left']]:
            self.player1.x = max(20, self.player1.x - SPEED)
        if keys[self.player1.controls['right']]:
            self.player1.x = min(SCREEN_WIDTH-20, self.player1.x + SPEED)
        if keys[self.player1.controls['jump']]:
            self.player1.jump()
        if keys[self.player1.controls['attack']]:
            self.player1.attack(self.player2)
            
        # 玩家2控制
        if keys[self.player2.controls['left']]:
            self.player2.x = max(20, self.player2.x - SPEED)
        if keys[self.player2.controls['right']]:
            self.player2.x = min(SCREEN_WIDTH-20, self.player2.x + SPEED)
        if keys[self.player2.controls['jump']]:
            self.player2.jump()
        if keys[self.player2.controls['attack']]:
            self.player2.attack(self.player1)

    def run(self):
        running = True
        while running:
            for event in pygame.event.get():
                if event.type == pygame.QUIT:
                    running = False
                    
            self.handle_events()
            
            # 更新角色状态
            self.player1.update(self.ground)
            self.player2.update(self.ground)
            
            # 绘制画面
            self.screen.fill(WHITE)
            pygame.draw.rect(self.screen, (100, 100, 100), (0, self.ground, SCREEN_WIDTH, 50))  # 地面
            
            self.player1.draw(self.screen)
            self.player2.draw(self.screen)
            
            # 显示得分
            font = pygame.font.Font(None, 36)
            text1 = font.render(f"Player1: {self.player1.score}", True, RED)
            text2 = font.render(f"Player2: {self.player2.score}", True, BLUE)
            self.screen.blit(text1, (20, 20))
            self.screen.blit(text2, (SCREEN_WIDTH-150, 20))
            
            pygame.display.flip()
            self.clock.tick(60)  # 60 FPS

        pygame.quit()
        sys.exit()

if __name__ == "__main__":
    game = Game()
    game.run()